﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.Numbers
{
    public static class NumbersExtension
    {
        public static bool isPositive(this object num)
        {
            if (IsNumeric(num) == false) throw new InvalidCastException("Object is not numeric.", new Exception(num.GetType().ToString()));
            dynamic tstobj = num;

            return tstobj > 0;
        }

        public static bool isNegative(this object num)
        {
            if (IsNumeric(num) == false) throw new InvalidCastException("Object is not numeric.", new Exception(num.GetType().ToString()));
            dynamic tstobj = num;

            return tstobj < 0;
        }

        public static bool isZero(this object num)
        {
            if (IsNumeric(num) == false) throw new InvalidCastException("Object is not numeric.", new Exception(num.GetType().ToString()));
            dynamic tstobj = num;

            return tstobj == 0;
        }

        public static bool IsNumeric(this object obj)
        {
            return (obj == null) ? false : IsNumeric(obj.GetType());
        }

        internal static bool IsNumeric(Type type)
        {
            if (type == null)
                return false;

            TypeCode typeCode = Type.GetTypeCode(type);

            switch (typeCode)
            {
                case TypeCode.Byte:
                case TypeCode.Decimal:
                case TypeCode.Double:
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                case TypeCode.SByte:
                case TypeCode.Single:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                case TypeCode.UInt64:
                    return true;
            }

            return false;
        }
    }
}
